﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DC2010;
using DC2010.Objects.Data;
using DC2010_EditorData;
using SdlDotNet.Core;
using SdlDotNet.Input;

namespace DC2012
{
    public partial class CoreEditor
    {
        //Handles keyboard events. 
        // The 'Escape' and 'Q'keys will cause the app to exit
        private void KeyboardDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Key.Escape || e.Key == Key.Q)
            {
                Events.QuitApplication();
            }

            if (e.Key == Key.A)
            {
                Root.I.GameSystem.ItemInHand = null;
            }

            if (e.Key == Key.R)
            {
                //rotate sprite.
                if (selectedTile != null)
                {
                    selectedTile.Surface = selectedTile.Surface.CreateRotatedSurface(90);

                    switch (((DCAnimatedSprite)selectedTile).Direction)
                    {
                        case Directions.South:
                            ((DCAnimatedSprite) selectedTile).Direction = Directions.East;
                            break;
                        case Directions.East:
                            ((DCAnimatedSprite)selectedTile).Direction = Directions.North;
                            break;
                        case Directions.North:
                            ((DCAnimatedSprite)selectedTile).Direction = Directions.West;
                            break;
                        case Directions.West:
                            ((DCAnimatedSprite)selectedTile).Direction = Directions.South;
                            break;
                    }
                }
            }

            if (e.Key == Key.X)
            {
                selectedTile = null;
            }

            if (e.Key == Key.C)
            {
                ChangeMapForm cmf = new ChangeMapForm();
                cmf.ShowDialog();

                if (!string.IsNullOrEmpty(cmf.newMapCode))
                {
                    ActiveMap = cmf.newMapCode;

                    FloorInfo fi = Root.I.FloorSystem.GetInfo(ActiveMap);
                    if (fi == null)
                    {
                        fi = new FloorInfo();
                        fi.Id = cmf.newMapCode;
                        Root.I.FloorSystem.AddInfo(fi);
                    }
                }

                map.Clear();
            }

            if (e.Key == Key.F)
            {
                var x = (from i in gameinterface
                         where (i is DCAnimatedSprite) && (((DCAnimatedSprite)i).Id == "TILE_FLOOR")
                         select i).FirstOrDefault();

                if (x != null)
                {
                    selectedTile = Neocortex.I.CreateNewSprite((DCAnimatedSprite)x);
                }
            }

        }

    }
}
